import SDKManager, { Platform } from "../sdk/SDKManager";
import { JSBridge } from "../sdk/JSBridge";

export default class Http {

    static isNetworkOK = true;

    static listenNetworkStatusChange() {
        SDKManager.instance.listenNetworkStatusChange((b: boolean) => {
            Http.isNetworkOK = b
        });
    }

    /** callback(success: boolean, res: string) */
    static get(url: string, callback: Function) {
        Http.request('GET', url, null, callback);
    }

    /** callback(success: boolean, res: string) */
    static post(url: string, data: {}, callback: Function) {
        Http.request('POST', url, data, callback);
    }

    private static request(method: string, url: string, data: {}, callback: Function) {
        if (url.indexOf('?') >= 0) {
            url += '&random=' + Math.random();
        } else {
            url += '?random=' + Math.random();
        }
        switch (SDKManager.platform) {
            case Platform.WeiXin:
                let wx = JSBridge.getWXObj();
                wx.request({
                    url: url,
                    method: method,
                    data: data,
                    success(res) {
                        console.log('http success res[ ' + url + ' ]: ' + JSON.stringify(res));
                        callback && callback(true, JSON.stringify(res.data));
                    },
                    fail(err) {
                        console.log('http fail res[ ' + url + ' ]: ' + JSON.stringify(err));
                        callback && callback(false, JSON.stringify(err));
                    }
                });
                break;
            case Platform.TouTiao:
                let tt = JSBridge.getTTObj();
                tt.request({
                    url: url,
                    method: method,
                    data: data,
                    success(res) {
                        console.log('http success res[ ' + url + ' ]: ' + JSON.stringify(res));
                        callback && callback(true, JSON.stringify(res.data));
                    },
                    fail(err) {
                        console.log('http fail res[ ' + url + ' ]: ' + JSON.stringify(err));
                        callback && callback(false, JSON.stringify(err));
                    }
                });
                break;
            default:
                let xhr: XMLHttpRequest = cc.loader.getXMLHttpRequest();
                xhr.timeout = 5000;
                xhr.open(method, url, true);
                if (cc.sys.isNative) {
                    xhr.setRequestHeader('Accept-Encoding', 'text/html;charset=UTF-8');
                }
                xhr.onreadystatechange = () => {
                    if (xhr.readyState === 4) {
                        console.log('http ' + method + ' res[ ' + url + ' ]: ' + xhr.responseText);
                        if (xhr.status == 200) {
                            if (callback) {
                                callback(true, xhr.responseText);
                            }
                        } else {
                            if (callback) {
                                callback(false, xhr.responseText);
                            }
                        }
                    }
                }
                let params = '';
                for (let key in data) {
                    if (params) {
                        params += '&';
                    }
                    params += key + '=' + data[key];
                }
                xhr.send(params);
                break;
        }
    }

}
